package org.tln.db137.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tln.db137.common.R;
import org.tln.db137.entity.Menu;
import org.tln.db137.service.MenuService;

import java.util.List;

@RestController
@CrossOrigin("*")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 获取所有菜单人教版
     */
    @GetMapping("selevtMenu")
    public R selevtMenu(){
        List<Menu> menuList = this.menuService.list();//获取所有的菜单

        return new R().add("menuList", menuList);
    }
    /**
     * 人教版selectmenubyid 根据id获取一个对象
     */
    @GetMapping("selectmenubyid/{id}")
    public R selectmenubyid(@PathVariable("id") int id){
        Menu menu = this.menuService.getById(id);
        return new R().add("menu",menu);
    }
    /**
     * 查询所有菜单
     */
    @GetMapping("/selectAllMenu")
    public R selectAll(Integer page, @RequestParam(required = false) String label) {
        PageHelper.startPage(page, 5);
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.like("label", label == null ? "" : label);
        List<Menu> list = menuService.list(wrapper);
        PageInfo<Menu> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo", pageInfo);
    }

    /**
     * 删除菜单
     */
    @PostMapping("/deleteMenu")
    public Object deleteMenu(@RequestBody Menu menu) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("id", menu.getId());
        return menuService.remove(wrapper);
    }

    /**
     * 新增菜单
     */
    @PostMapping("/insertmeun")
    public Object insertMenu(@RequestBody Menu menu) {
        System.out.println("menu = " + menu);
        return menuService.save(menu);
    }

    /**
     * 查询所有菜单的菜单
     */
    @GetMapping("/selectMenu")
    public R selectAll(Integer all) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("parentid", 0);
        List<Menu> pageInfo = menuService.list(all == 1 ? wrapper : null);
        return new R().add("pageInfo", pageInfo);
    }
    /**
     * 修改
     */
    @PostMapping("/updatemeun")
    public Object updatemeun(@RequestBody Menu menu) {
        return menuService.updateById(menu);
    }
}
